<template>
  <div>
    <a-page-header
      style="border: 1px solid rgb(235, 237, 240)"
      title="新建小程序"
      :ghost="false"
      @back="() => $router.go(-1)"
    ></a-page-header>
    <div class="body">
      <div class="item" @click="authorization">
        <div class="content">
          <img src="../../assets/images/code.png" alt="" />
          <div class="title">
            <span>直接授权</span>
            <span v-if="type == 0">适用用户：已有企业微信小程序的账号用户，可直接授权发布微信小程序</span>
            <span v-if="type == 1">适用用户：已有支付宝小程序的账号用户，可直接授权发布支付宝小程序</span>
          </div>
        </div>
        <div class="foot">申请小程序</div>
      </div>
      <div class="item" @click="goApply">
        <div class="content">
          <img src="../../assets/images/reg.png" alt="" />
          <div class="title">
            <span>直接注册小程序</span>
            <span>适用用户：所有用户/注册小程序耗时：15分钟/认证耗时：约1-5天</span>
          </div>
        </div>
        <div class="foot">申请小程序</div>
      </div>
    </div>
  </div>
</template>

<script>
import { authorizationConfig, alipayAuthorization } from '@/api/setting/applet'
export default {
  data() {
    return {
      // 类型  0为微信 1是支付宝
      type: 0,
      hasChannel: false,
    }
  },
  methods: {
    // 点击授权
    async authorization() {
      if (this.type == 0) {
        const res = await authorizationConfig()
        if (res.code == 0) {
          window.location.href = res.url
        }
      }
      if (this.type == 1) {
        const res = await alipayAuthorization()
        if (res.code == 0) {
          window.location.href = res.url
        }
      }
    },

    goApply() {
      let { type, hasChannel } = this
      if (!parseInt(type)) {
        if (hasChannel) return this.$router.push('/saasChannel/applet/writeInformation?type=' + type)
        this.$router.push('/setting/appletManagement/writeInformation?type=' + type)
      } else {
        if (hasChannel) return this.$router.push('/saasChannel/appletAlipay/writeInformationAlipay')
        this.$router.push('/setting/appletManagement/writeInformationAlipay')
      }
    },
  },
  mounted() {
    if (this.$route.query.type) {
      this.type = this.$route.query.type
    }

    if (this.$route.meta.hasChannel) {
      this.hasChannel = true
    }
  },
}
</script>

<style lang="less" scoped>
.body {
  padding: 24px;
  display: flex;
  align-items: center;
  .item {
    background: #fff;
    width: 363px;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.45);
    border-radius: 2px;
    border: 1px solid #e9e9e9;
    cursor: pointer;
    margin-right: 23px;
    .content {
      display: flex;
      padding: 24px 44px 33px 24px;
      img {
        width: 48px;
        height: 48px;
        margin-right: 16px;
        border-radius: 50%;
      }
      .title {
        display: flex;
        flex-direction: column;
        padding: 0;
        font-size: 14px;
        span:nth-child(1) {
          color: rgba(0, 0, 0, 0.85);
          font-size: 16px;
          margin-bottom: 12px;
        }
      }
    }
    .foot {
      height: 48px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-top: 1px solid #e9e9e9;
      background-color: #f7f9fa;
    }
  }
}
</style>
